
angular.module('U9ImHd.controllers')

.controller('LoginCtrl', ['$scope', 'imanUsbPort', 'User', 'Help', 
    function ($scope, imanUsbPort, User, Help) {
        $scope.data = {
            activeIndex: 1,
            usercode: '',
            password: '',
            autologin: false
        };

        $scope.clickTab = function (index) {
            $scope.data.activeIndex = index;
            if (index === 2) {
                imanUsbPort.start();
            } else {
                imanUsbPort.stop();
            }
        };
        $scope.signin = function () {
            User.signin(
                $scope.data.usercode, 
                $scope.data.password, 
                $scope.data.autologin
            ).then(function () {
                Help.goAsRoot('home');
            });
        };

        $scope.$on('imanUsbPort.input', function (e, inputStr) {
            User.signinScan(inputStr).then(function () {
                Help.goAsRoot('home');
            });
        });

        $scope.$on('$ionicView.afterLeave', function () {
            imanUsbPort.stop();
        });
    }
]);